package com.zsck.zsgy.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.zsck.commonbase.util.RotateTransformation;
import com.zsck.zsgy.R;

import java.io.ByteArrayOutputStream;


/**
 * 创建者     Bowllboy
 * 创建时间   2020/4/23 16:13
 * 描述	      ${TODO}
 */
public class GlideUtils {
    public static void display(String url, ImageView imageView, Context context){
//        GlideApp.with(context).load(url).dontAnimate().centerCrop().into(imageView);
        GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder()
                .addHeader("sourceType", "h5")
                .addHeader( "Authorization", PrefserHelper.getUserKey()
                )
                .build());

        Glide.with(context).load(glideUrl).dontAnimate().optionalFitCenter().into(imageView);
    }



    public static void displayWithHeader(String url, ImageView imageView, Context context){
        LogUtils.e("displayWithHeader :"+url);
        GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder()
                .addHeader("sourceType", "h5")
                .addHeader( "Authorization", PrefserHelper.getUserKey()
                )
                .build());
        Glide.with(context)
                .load(glideUrl)
                .apply(RequestOptions.bitmapTransform(new RotateTransformation(180f)))
                .dontAnimate()
                .centerCrop()

                .placeholder(R.mipmap.certificates_1_1)
                .disallowHardwareConfig()
                .into(imageView);
    }
    public static void displayNoCenter(String url, ImageView imageView, Context context){
        GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder()
                .addHeader("sourceType", "h5")
                .addHeader( "Authorization", PrefserHelper.getUserKey()
                )
                .build());
        Glide.with(context).load(glideUrl).dontAnimate().optionalFitCenter().into(imageView);
    }
    public static void disPlayFromOSSWithConner(String url, ImageView imageView, Context context, int conner){
        String imageUrl = Constants.Companion.getIMG_HOST() + url;
        GlideUrl glideUrl = new GlideUrl(imageUrl, new LazyHeaders.Builder()
                .addHeader("sourceType", "h5")
                .addHeader( "Authorization", PrefserHelper.getUserKey()
                )
                .build());
        Glide.with(context).load(glideUrl).dontAnimate().optionalFitCenter().into(imageView);
    }

    /**
     * 加载图片到background
     * @param url
     * @param imageView
     * @param context
     * @param conner
     */
    public static void disPlayFromOSSWithBg(String url, ImageView imageView, Context context, int conner){
        String imageUrl =Constants.Companion.getIMG_HOST()  + url;
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(conner);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        options.error(R.mipmap.default_h);
        options.placeholder(R.mipmap.default_h);

        GlideUrl glideUrl = new GlideUrl(imageUrl, new LazyHeaders.Builder()
                .addHeader("sourceType", "h5")
                .addHeader( "Authorization", PrefserHelper.getUserKey()
                )
                .build());
        Glide.with(context).load(glideUrl).apply(options).into(imageView);
    }


    public static void display(int id, ImageView imageView, Context context){
        Glide.with(context).load(id).centerCrop().into(imageView);

    }
    public static void display(Bitmap bitmap, ImageView imageView, Context context){
        Glide.with(context).load(bitmap).centerCrop().into(imageView);
    }

    public static void displayWithConner(String url, ImageView imageView, Context context, int conner){
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(conner);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        options.error(R.mipmap.default_h);
        options.placeholder(R.mipmap.default_h);
//        GlideApp.with(context).load(url).apply(options).into(imageView);

        GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder()
                .addHeader("sourceType", "h5")
                .addHeader( "Authorization", PrefserHelper.getUserKey()
                )
                .build());
        Glide.with(context).load(glideUrl).apply(options).into(imageView);
    }
    public static void displayWithConnerDefault(String url, ImageView imageView, Context context, int conner, int defaultid){
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(conner);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        options.error(defaultid);
        options.transform(new CenterCrop(),roundedCorners);
        options.placeholder(defaultid);

//        GlideApp.with(context).load(url).apply(options).into(imageView);

        GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder()
                .addHeader("sourceType", "h5")
                .addHeader( "Authorization", PrefserHelper.getUserKey()
                )
                .build());
        Glide.with(context).load(glideUrl).apply(options).into(imageView);
    }
    public static void displayWithIdConner(int id, ImageView imageView, Context context, int conner){
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(conner);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
//                .override(300, 300);
        Glide.with(context).load(id).apply(options).into(imageView);
    }
    public  static void displayWithCircle(String url, ImageView imageView, Context context){
        RequestOptions options = RequestOptions.circleCropTransform();
//        GlideApp.with(context).load(url).apply(options).into(imageView);


        GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder()
                .addHeader("sourceType", "h5")
                .addHeader( "Authorization", PrefserHelper.getUserKey()
                )
                .build());
        Glide.with(context).load(glideUrl).apply(options).into(imageView);
    }
    public static void DipslayBitmapWithCircle(Bitmap bitmap, ImageView imageView, Context context){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes=baos.toByteArray();
        RequestOptions options = RequestOptions.circleCropTransform();
        Glide.with(context).load(bytes).apply(options).into(imageView);

    }
}
